package com.chinairi.mall.service.impl;

import com.chinairi.common.utils.DateUtils;
import com.chinairi.mall.service.modelobject.OCTAForSPMO;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.OrderCommodityTrackAlarm;
import com.chinairi.mall.dao.OrderCommodityTrackAlarmMapper;
import com.chinairi.mall.service.IOrderCommodityTrackAlarmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 预警表 服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-08-19
 */
@Service
public class OrderCommodityTrackAlarmServiceImpl extends ServiceImpl<OrderCommodityTrackAlarmMapper, OrderCommodityTrackAlarm> implements IOrderCommodityTrackAlarmService {
    @Autowired
    private OrderCommodityTrackAlarmMapper orderCommodityTrackAlarmMapper;

    /**
     * 单独手动修改预警信息。
     *
     * @param orderCommodityTrackAlarm
     */
    @Override
    public void update(OrderCommodityTrackAlarm orderCommodityTrackAlarm) {
        orderCommodityTrackAlarmMapper.updateById(orderCommodityTrackAlarm);
    }

    /**
     * 把中台中的数据获取出来并放入数据库
     */
    @Override
    public void updateFirstByServiceProvider() {
        /*
        预计接收一个实体，对象根据后续中台设计而定。
         */
    }

    /**
     * 根据中台的数据，获取生产计划时间，据此配置数据库。此时由于原先仅仅配置了第一个节点，现在需要顺便配置所有节点。
     *
     * @param octaForSpmoList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByServiceProviderPlan(List<OCTAForSPMO> octaForSpmoList) {
        if (octaForSpmoList.size() > 0) {
            orderCommodityTrackAlarmMapper.batchUpdate(octaForSpmoList.stream().peek((list) -> {
                list.setAlarmDate(DateUtils.stringToDate(DateUtils.getDateOperation(list.getPlanDate(), -list.getPre()), "yyyy-MM-dd 00:00:00"));
            }).collect(Collectors.toList()));
        } else {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "回传数据为空");
        }
    }
}
